<?php
import('$/util/hash_type.php');
/**
 * 处理分类交集
 */
class MixController extends Controller
{
    public function show()
	{
		$rid	= fetch_get('rid','int');
		$where	= $rid ? "(r1={$rid} OR r2={$rid})" : '';
		$list	= Db::get('CateMix')->select()->where($where)->order('pid1 ASC,pid2 ASC,hid ASC')->getAll();
#echo Db::get('CateMix')->lastSql();
		foreach($list as &$r)
		{
			$r['_pid1']	= $r['pid1'];
			$r['_pid2']	= $r['pid2'];
			$r['value']	= CateMix::getMix($r['pid1'], $r['pid2']);
			$r['pid1']	= Cate::cachedGetPath($r['pid1']);
			$r['pid2']	= Cate::cachedGetPath($r['pid2']);
			$r['hid']	= Cate::cachedGetPath($r['hid']);
		}
		$this->assign('list',	$list);
		$cates	= Db::get('Cate')->select()
			->where(array('pid'=>0,'off'=>0))
			->order('sort ASC,hid ASC')->hkey('hid')->getAll();
		foreach($cates as &$c)
		{
			if($c['type']==NODE_TYPE_MIX){
				$c['title']	.= ' (交)';
			}
		}
		$this->assign('cates',	$cates);
		$this->display();
	}

	/**
	 *
	 * @todo 分类一里出的下拉列表，若跟分类二的root有交集，显示到分类二的下拉里。
	 * 
	 */
	public function add()
	{
		if(!empty($_POST))
		{
			$pid1	= fetch_post('pid1', 'int');
			$pid2	= fetch_post('pid2', 'int');
			$hid	= fetch_post('hid', 'int');
			$type	= fetch_post('type', 'int');
			$val	= fetch_post('value', 'array');

			if(!$pid1 && !$pid2){
				Show::warning('Both pid1 and pid2 are empty.');
			}
			if(!$hid){
				Show::warning('Choose a item from HID options.');
			}
			if($type==1 && !$val){
				Show::warning('Check on mix items.');
			}
			if($pid1==$pid2){
				Show::warning('Choose different pids.');
			}

			if($type==1){
				$r	= CateMix::addLink($pid1, $pid2, $hid, join(',',$val));
			}
			else{
				$r	= CateMix::addFix($pid1, $pid2, $hid);
			}

			if(!$r){
				Show::warning('Error!');
			}
			else{
				Show::notice('OK');
			}
		}

		$cates	= Db::get('Cate')->select('title')
					->where(array('pid'=>0,'off'=>0))
					->order('sort ASC,hid ASC')->hkey('hid')->getCols();
		$mixes	= Db::get('Cate')->select('title')
					->where(array('pid'=>0,'off'=>0,'type'=>NODE_TYPE_MIX))
					->order('sort ASC,hid ASC')->hkey('hid')->getCols();
		foreach($mixes as $k=>$_){
			$cates[$k]	.= ' (交)';
		}
		$this->assign('cates',	$cates);
		$this->assign('mixes',	$mixes);
		$this->display();
	}

	/**
	 * Get Cates by Cate Root ID.
	 * @param $ref_id	reference by CateMix.r1/2
	 */
	public function ci()
	{
		$id		= fetch_get('id', 'int');
		$ref_id	= fetch_get('ref', 'int');
		if(!$id) return ;
		if($ref_id){
			$refs	= CateMix::getMix($ref_id, 0);
			if(!$refs){
				return Cate::getByPid($id);
			}
			$tmp	= array();
			foreach($refs as $hid=>$cate)
			{
				if($cate['pid']==$id){
					$tmp[$hid]	= $cate;
				}
			}
			Show::ajaxBand(array('items'=>$tmp?$tmp:Cate::getByPid($id)));
			Show::notice('ok');
		}
		else{
			Show::ajaxBand(array('items'=>Cate::getByPid($id)));
			Show::notice('ok');
		}
	}

	/**
	 * find mixed records by pid1 with pid2
	 */
	public function find()
	{
		list($pid1, $pid2)	= explode(',', fetch_get('pid'));
		$list	= CateMix::getMix($pid1, $pid2);
		if(!$list){
			Show::warning('mixed records not found.');
		}
		Show::ajaxBand(array('mixed'=>$list));
		Show::notice('ok');
	}

	public function del()
	{
		list($pid1, $pid2)	= explode(',', fetch_get('pid'));
		if(CateMix::delete($pid1, $pid2))
			Show::notice('ok');
		else
			Show::warning('failed');
	}

}
?>